<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Decorator</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../using_websocket.html" title="WebSocket">
<link rel="prev" href="handshaking.html" title="Handshaking">
<link rel="next" href="messages.html" title="Messages">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="handshaking.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="messages.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.using_websocket.decorator"></a><a class="link" href="decorator.html" title="Decorator">Decorator</a>
</h3></div></div></div>
<p>
        For programs which need to modify either the outgoing WebSocket HTTP Upgrade
        request, the outgoing WebSocket HTTP Upgrade response, or both, the stream
        supports an optional property called a <span class="emphasis"><em>decorator</em></span>. This
        is a function pointer or callable object which is invoked before the implementation
        sends an HTTP message. The decorator receives a modifiable reference to the
        message, allowing for modifications. The interface to this system uses:
      </p>
<div class="table">
<a name="beast.using_websocket.decorator.websocket_decorator_interface"></a><p class="title"><b>Table 1.32. WebSocket Decorator Interface</b></p>
<div class="table-contents"><table class="table" summary="WebSocket Decorator Interface">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Name
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__websocket__request_type.html" title="websocket::request_type"><code class="computeroutput"><span class="identifier">request_type</span></code></a>
                </p>
              </td>
<td>
                <p>
                  This is the type of the object passed to the decorator to represent
                  HTTP Upgrade requests.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__websocket__response_type.html" title="websocket::response_type"><code class="computeroutput"><span class="identifier">response_type</span></code></a>
                </p>
              </td>
<td>
                <p>
                  This is the type of the object passed to the decorator to represent
                  HTTP Upgrade response.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__websocket__stream_base__decorator.html" title="websocket::stream_base::decorator"><code class="computeroutput"><span class="identifier">stream_base</span><span class="special">::</span><span class="identifier">decorator</span></code></a>
                </p>
              </td>
<td>
                <p>
                  Objects of this type are used to hold a decorator to be set on
                  the stream using <code class="computeroutput"><span class="identifier">set_option</span></code>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__websocket__stream/set_option.html" title="websocket::stream::set_option"><code class="computeroutput"><span class="identifier">stream</span><span class="special">::</span><span class="identifier">set_option</span></code></a>
                </p>
              </td>
<td>
                <p>
                  This function is used to set a <code class="computeroutput"><span class="identifier">stream_base</span><span class="special">::</span><span class="identifier">decorator</span></code>
                  on the stream.
                </p>
              </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
        This declares a normal function which decorates outgoing HTTP requests:
      </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">set_user_agent</span><span class="special">(</span><span class="identifier">request_type</span><span class="special">&amp;</span> <span class="identifier">req</span><span class="special">)</span>
<span class="special">{</span>
    <span class="comment">// Set the User-Agent on the request</span>
    <span class="identifier">req</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">user_agent</span><span class="special">,</span> <span class="string">"My User Agent"</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
        When using a decorator, it must be set on the stream before any handshaking
        takes place. This sets the decorator on the stream, to be used for all subsequent
        calls to accept or handshake:
      </p>
<pre class="programlisting"><span class="identifier">stream</span><span class="special">&lt;</span><span class="identifier">tcp_stream</span><span class="special">&gt;</span> <span class="identifier">ws</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">);</span>

<span class="comment">// The function `set_user_agent` will be invoked with</span>
<span class="comment">// every upgrade request before it is sent by the stream.</span>

<span class="identifier">ws</span><span class="special">.</span><span class="identifier">set_option</span><span class="special">(</span><span class="identifier">stream_base</span><span class="special">::</span><span class="identifier">decorator</span><span class="special">(&amp;</span><span class="identifier">set_user_agent</span><span class="special">));</span>
</pre>
<p>
        Alternatively, a function object may be used. Small function objects will
        not incur a memory allocation. The follow code declares and sets a function
        object as a decorator:
      </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">set_server</span>
<span class="special">{</span>
    <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">response_type</span><span class="special">&amp;</span> <span class="identifier">res</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Set the Server field on the response</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">user_agent</span><span class="special">,</span> <span class="string">"My Server"</span><span class="special">);</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="identifier">ws</span><span class="special">.</span><span class="identifier">set_option</span><span class="special">(</span><span class="identifier">stream_base</span><span class="special">::</span><span class="identifier">decorator</span><span class="special">(</span><span class="identifier">set_server</span><span class="special">{}));</span>
</pre>
<p>
        A lambda may be used in place of a named function object:
      </p>
<pre class="programlisting"><span class="identifier">ws</span><span class="special">.</span><span class="identifier">set_option</span><span class="special">(</span><span class="identifier">stream_base</span><span class="special">::</span><span class="identifier">decorator</span><span class="special">(</span>
    <span class="special">[](</span><span class="identifier">response_type</span><span class="special">&amp;</span> <span class="identifier">res</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Set the Server field on the response</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">user_agent</span><span class="special">,</span> <span class="string">"My Server"</span><span class="special">);</span>
    <span class="special">}));</span>
</pre>
<p>
        It also possible for a single decorator to handle both requests and responses,
        if it is overloaded for both types either as a generic lambda (C++14 and
        later) or as a class as shown here:
      </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">set_message_fields</span>
<span class="special">{</span>
    <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">request_type</span><span class="special">&amp;</span> <span class="identifier">req</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Set the User-Agent on the request</span>
        <span class="identifier">req</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">user_agent</span><span class="special">,</span> <span class="string">"My User Agent"</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">response_type</span><span class="special">&amp;</span> <span class="identifier">res</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Set the Server field on the response</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">user_agent</span><span class="special">,</span> <span class="string">"My Server"</span><span class="special">);</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="identifier">ws</span><span class="special">.</span><span class="identifier">set_option</span><span class="special">(</span><span class="identifier">stream_base</span><span class="special">::</span><span class="identifier">decorator</span><span class="special">(</span><span class="identifier">set_message_fields</span><span class="special">{}));</span>
</pre>
<p>
        The implementation takes ownership by decay-copy of the invocable object
        used as the decorator. Move-only types are possible:
      </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">set_auth</span>
<span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">key</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">request_type</span><span class="special">&amp;</span> <span class="identifier">req</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Set the authorization field</span>
        <span class="identifier">req</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">authorization</span><span class="special">,</span> <span class="special">*</span><span class="identifier">key</span><span class="special">);</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="comment">// The stream takes ownership of the decorator object</span>
<span class="identifier">ws</span><span class="special">.</span><span class="identifier">set_option</span><span class="special">(</span><span class="identifier">stream_base</span><span class="special">::</span><span class="identifier">decorator</span><span class="special">(</span>
    <span class="identifier">set_auth</span><span class="special">{</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">make_unique</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="string">"Basic QWxhZGRpbjpPcGVuU2VzYW1l"</span><span class="special">)}));</span>
</pre>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
          Undefined behavior results if the decorator modifies the fields specific
          to perform the WebSocket Upgrade, such as the Upgrade or Connection fields.
        </p></td></tr>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="handshaking.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="messages.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
